/*
 * 
 */
package ged.diagram.part;

import ged.Diagram;
import ged.GedPackage;
import ged.Link;
import ged.Node;
import ged.diagram.edit.parts.Diagram2EditPart;
import ged.diagram.edit.parts.DiagramEditPart;
import ged.diagram.edit.parts.LinkEditPart;
import ged.diagram.edit.parts.NodeEditPart;
import ged.diagram.providers.GedElementTypes;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.gmf.runtime.notation.View;

/**
 * @generated
 */
public class GedDiagramUpdater {

	/**
	 * @generated
	 */
	public static boolean isShortcutOrphaned(View view) {
		return !view.isSetElement() || view.getElement() == null
				|| view.getElement().eIsProxy();
	}

	/**
	 * @generated
	 */
	public static List<GedNodeDescriptor> getSemanticChildren(View view) {
		switch (GedVisualIDRegistry.getVisualID(view)) {
		case DiagramEditPart.VISUAL_ID:
			return getDiagram_1000SemanticChildren(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<GedNodeDescriptor> getDiagram_1000SemanticChildren(
			View view) {
		if (!view.isSetElement()) {
			return Collections.emptyList();
		}
		Diagram modelElement = (Diagram) view.getElement();
		LinkedList<GedNodeDescriptor> result = new LinkedList<GedNodeDescriptor>();
		for (Iterator<?> it = modelElement.getNodes().iterator(); it.hasNext();) {
			Node childElement = (Node) it.next();
			int visualID = GedVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == Diagram2EditPart.VISUAL_ID) {
				result.add(new GedNodeDescriptor(childElement, visualID));
				continue;
			}
			if (visualID == NodeEditPart.VISUAL_ID) {
				result.add(new GedNodeDescriptor(childElement, visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List<GedLinkDescriptor> getContainedLinks(View view) {
		switch (GedVisualIDRegistry.getVisualID(view)) {
		case DiagramEditPart.VISUAL_ID:
			return getDiagram_1000ContainedLinks(view);
		case Diagram2EditPart.VISUAL_ID:
			return getDiagram_2003ContainedLinks(view);
		case NodeEditPart.VISUAL_ID:
			return getNode_2001ContainedLinks(view);
		case LinkEditPart.VISUAL_ID:
			return getLink_4001ContainedLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<GedLinkDescriptor> getIncomingLinks(View view) {
		switch (GedVisualIDRegistry.getVisualID(view)) {
		case Diagram2EditPart.VISUAL_ID:
			return getDiagram_2003IncomingLinks(view);
		case NodeEditPart.VISUAL_ID:
			return getNode_2001IncomingLinks(view);
		case LinkEditPart.VISUAL_ID:
			return getLink_4001IncomingLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<GedLinkDescriptor> getOutgoingLinks(View view) {
		switch (GedVisualIDRegistry.getVisualID(view)) {
		case Diagram2EditPart.VISUAL_ID:
			return getDiagram_2003OutgoingLinks(view);
		case NodeEditPart.VISUAL_ID:
			return getNode_2001OutgoingLinks(view);
		case LinkEditPart.VISUAL_ID:
			return getLink_4001OutgoingLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<GedLinkDescriptor> getDiagram_1000ContainedLinks(
			View view) {
		Diagram modelElement = (Diagram) view.getElement();
		LinkedList<GedLinkDescriptor> result = new LinkedList<GedLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_Link_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<GedLinkDescriptor> getDiagram_2003ContainedLinks(
			View view) {
		Diagram modelElement = (Diagram) view.getElement();
		LinkedList<GedLinkDescriptor> result = new LinkedList<GedLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_Link_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<GedLinkDescriptor> getNode_2001ContainedLinks(View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<GedLinkDescriptor> getLink_4001ContainedLinks(View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<GedLinkDescriptor> getDiagram_2003IncomingLinks(View view) {
		Diagram modelElement = (Diagram) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<GedLinkDescriptor> result = new LinkedList<GedLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_Link_4001(modelElement,
				crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<GedLinkDescriptor> getNode_2001IncomingLinks(View view) {
		Node modelElement = (Node) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<GedLinkDescriptor> result = new LinkedList<GedLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_Link_4001(modelElement,
				crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<GedLinkDescriptor> getLink_4001IncomingLinks(View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<GedLinkDescriptor> getDiagram_2003OutgoingLinks(View view) {
		Diagram modelElement = (Diagram) view.getElement();
		LinkedList<GedLinkDescriptor> result = new LinkedList<GedLinkDescriptor>();
		result.addAll(getOutgoingTypeModelFacetLinks_Link_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<GedLinkDescriptor> getNode_2001OutgoingLinks(View view) {
		Node modelElement = (Node) view.getElement();
		LinkedList<GedLinkDescriptor> result = new LinkedList<GedLinkDescriptor>();
		result.addAll(getOutgoingTypeModelFacetLinks_Link_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<GedLinkDescriptor> getLink_4001OutgoingLinks(View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	private static Collection<GedLinkDescriptor> getContainedTypeModelFacetLinks_Link_4001(
			Diagram container) {
		LinkedList<GedLinkDescriptor> result = new LinkedList<GedLinkDescriptor>();
		for (Iterator<?> links = container.getLinks().iterator(); links
				.hasNext();) {
			EObject linkObject = (EObject) links.next();
			if (false == linkObject instanceof Link) {
				continue;
			}
			Link link = (Link) linkObject;
			if (LinkEditPart.VISUAL_ID != GedVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Node dst = link.getTarget();
			Node src = link.getSource();
			result.add(new GedLinkDescriptor(src, dst, link,
					GedElementTypes.Link_4001, LinkEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection<GedLinkDescriptor> getIncomingTypeModelFacetLinks_Link_4001(
			Node target,
			Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences) {
		LinkedList<GedLinkDescriptor> result = new LinkedList<GedLinkDescriptor>();
		Collection<EStructuralFeature.Setting> settings = crossReferences
				.get(target);
		for (EStructuralFeature.Setting setting : settings) {
			if (setting.getEStructuralFeature() != GedPackage.eINSTANCE
					.getLink_Target()
					|| false == setting.getEObject() instanceof Link) {
				continue;
			}
			Link link = (Link) setting.getEObject();
			if (LinkEditPart.VISUAL_ID != GedVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Node src = link.getSource();
			result.add(new GedLinkDescriptor(src, target, link,
					GedElementTypes.Link_4001, LinkEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection<GedLinkDescriptor> getOutgoingTypeModelFacetLinks_Link_4001(
			Node source) {
		Diagram container = null;
		// Find container element for the link.
		// Climb up by containment hierarchy starting from the source
		// and return the first element that is instance of the container class.
		for (EObject element = source; element != null && container == null; element = element
				.eContainer()) {
			if (element instanceof Diagram) {
				container = (Diagram) element;
			}
		}
		if (container == null) {
			return Collections.emptyList();
		}
		LinkedList<GedLinkDescriptor> result = new LinkedList<GedLinkDescriptor>();
		for (Iterator<?> links = container.getLinks().iterator(); links
				.hasNext();) {
			EObject linkObject = (EObject) links.next();
			if (false == linkObject instanceof Link) {
				continue;
			}
			Link link = (Link) linkObject;
			if (LinkEditPart.VISUAL_ID != GedVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Node dst = link.getTarget();
			Node src = link.getSource();
			if (src != source) {
				continue;
			}
			result.add(new GedLinkDescriptor(src, dst, link,
					GedElementTypes.Link_4001, LinkEditPart.VISUAL_ID));
		}
		return result;
	}

}
